package com.apes.fico.engine.service;

import com.apes.fico.engine.ElementStrategy;
import com.apes.fico.engine.model.Element;
import com.apes.fico.engine.repository.ElementRepository;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import com.apes.framework.util.MapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @ClassName ElementService
 * @Description TODO
 * @Author pengyb
 * @Date 2019/1/26 16:51
 * @Version 1.0
 **/
@Service("elementService")
public class ElementService extends DomainService {
    @Autowired
    private ElementRepository elementRepository;

    /**
     * 保存
     *
     * @param request
     * @return
     */
    public Element create(SimpleRequest request) {
        Element element = request.getO(Element.class);
        return elementRepository.saveAndFlush(element);
    }

    /**
     * 更新
     *
     * @param request
     * @return
     */
    public Element update(SimpleRequest request) {
        Element element = request.getO(Element.class);
        return elementRepository.saveAndFlush(element);
    }

    /**
     * 删除
     *
     * @param request
     * @return
     */
    public String delete(SimpleRequest request) {
        Element element = elementRepository.findOne((String) request.get("id"));
        elementRepository.delete(element.getId());
        return "删除成功";
    }

    public List<Map> getStrategy(SimpleRequest request) {
        return SpringManager.getBeansOfType(ElementStrategy.class).values().stream()
                .map(es -> MapUtil.mapper("id", es.getClass().getName(), "name", es.getDescription()))
                .collect(Collectors.toList());
    }

}
